from django.shortcuts import render
from system.models import LoginLog,OperationLog
from django.core.paginator import Paginator
from django.http import JsonResponse
from cmdb.settings import default_page,default_limit
from django.contrib.auth.decorators import login_required

def loginLog_page(request):
    return render(request, 'system/logs/loginLog.html')

def operationLog_page(request):
    return render(request, 'system/logs/operationLog.html')

@login_required()
def getLoginLogData(request):
    try:
        loginLog_list = LoginLog.objects.all()
        loginLog_count = LoginLog.objects.count()
        list_loginLog=[]
    except Exception as e:
        return JsonResponse({'code': 4000, 'msg': '查询数据失败！'})
    for i in loginLog_list:
        dict = {}
        dict['id'] = i.id
        dict['login_type'] = i.login_type
        dict['username'] = i.username
        dict['ip'] = i.ip 
        dict['agent'] = i.agent
        dict['browser'] = i.browser 
        dict['os'] = i.os 
        dict['create_datetime'] = i.create_datetime.strftime("%Y-%m-%d %H:%M:%S") 
        list_loginLog.append(dict)
    pageIndex = request.GET.get('page')  # pageIndex = request.POST.get('pageIndex')
    pageSize = request.GET.get('limit')  # pageSize = request.POST.get('pageSize')
    if pageIndex is None:
        pageIndex = default_page
    if pageSize is None:
        pageSize = default_limit
    pageInator = Paginator(list_loginLog, pageSize)
    contacts = pageInator.page(pageIndex)
    res = []
    for contact in contacts:
        res.append(contact)
    Result = {'code': 0, 'msg': '成功！','count':loginLog_count, 'data': res}
    return JsonResponse(Result)

@login_required()
def getOperationLogData(request):
    try:
        operaLog_list = OperationLog.objects.all()
        operaLog_count = OperationLog.objects.count()
        list_operaLog=[]
    except Exception as e:
        return JsonResponse({'code': 4000, 'msg': '查询数据失败！'})
    for i in operaLog_list:
        dict = {}
        dict['id'] = i.id
        dict['request_path'] = i.request_path
        dict['request_body'] = i.request_body 
        dict['request_method'] = i.request_method
        dict['request_ip'] = i.request_ip 
        dict['request_browser'] = i.request_browser 
        dict['response_code'] = i.response_code 
        dict['request_os'] = i.request_os 
        dict['json_result'] = i.json_result 
        dict['status'] = i.status 
        dict['create_datetime'] = i.create_datetime.strftime("%Y-%m-%d %H:%M:%S") 
        list_operaLog.append(dict)
    pageIndex = request.GET.get('page')  # pageIndex = request.POST.get('pageIndex')
    pageSize = request.GET.get('limit')  # pageSize = request.POST.get('pageSize')
    if pageIndex is None:
        pageIndex = default_page
    if pageSize is None:
        pageSize = default_limit
    pageInator = Paginator(list_operaLog, pageSize)
    contacts = pageInator.page(pageIndex)
    res = []
    for contact in contacts:
        res.append(contact)
    Result = {'code': 0, 'msg': '成功！','count':operaLog_count, 'data': res}
    return JsonResponse(Result)